{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# B-scan from a metal cylinder (2D)\n",
    "\n",
    "This example uses the same geometry as the previous (A-scan) example but this time a B-scan is created. A B-scan is composed of multiple traces (A-scans) recorded as the source and receiver are moved over the target, in this case the metal cylinder. The input needed to create the model is:\n",
    "\n",
    "### my_cylinder_Bscan_2D.in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%%writefile ../../user_models/cylinder_Bscan_2D.in\n",
    "#title: B-scan from a metal cylinder buried in a dielectric half-space\n",
    "#domain: 0.240 0.210 0.002\n",
    "#dx_dy_dz: 0.002 0.002 0.002\n",
    "#time_window: 3e-9\n",
    "\n",
    "#material: 6 0 1 0 half_space\n",
    "\n",
    "#waveform: ricker 1 1.5e9 my_ricker\n",
    "#hertzian_dipole: z 0.040 0.170 0 my_ricker\n",
    "#rx: 0.080 0.170 0\n",
    "#src_steps: 0.002 0 0\n",
    "#rx_steps: 0.002 0 0\n",
    "\n",
    "#box: 0 0 0 0.240 0.170 0.002 half_space\n",
    "#cylinder: 0.120 0.080 0 0.120 0.080 0.002 0.010 pec"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The differences between this input file and the one from the A-scan are the x coordinates of the source and receiver, and the commands needed to move the source and receiver. As before, the source and receiver are offset by 40mm from each other as before but they are now shifted to a starting position for the scan. The ``#src_steps`` command is used to move every source in the model by specified steps each time the model is run. Similarly, the ``#rx_steps`` command is used to move every receiver in the model by specified steps each time the model is run. Note, the same functionality can be achieved by using a block of Python code in the input file to move the source and receiver individually (for further details see the Python section of the User Guide)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Run the model\n",
    "\n",
    "To run the model to create a B-scan you must pass an optional argument to specify the number of times the model should be run. In this case this is the number of A-scans (traces) that will comprise the B-scan. For a B-scan over a distance of 120mm with a step of 2mm that is 60 A-scans. You can now run the model using:\n",
    "    \n",
    "    python -m gprMax user_models/cylinder_Bscan_2D.in -n 60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import os\n",
    "from gprMax.gprMax import api\n",
    "\n",
    "filename = os.path.join(os.pardir, os.pardir, 'user_models', 'cylinder_Bscan_2D.in')\n",
    "api(filename, n=60, geometry_only=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## View the results\n",
    "\n",
    "### Merge A-scans into B-scan\n",
    "\n",
    "You should have produced 60 output files, one for each A-scan, with names ``my_cylinder_Bscan_2D1.out``, ``my_cylinder_Bscan_2D2.out`` etc... These can be combined into a single file using the command:\n",
    "\n",
    "    python -m tools.outputfiles_merge user_models/cylinder_Bscan_2D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%run -m tools.outputfiles_merge user_models/cylinder_Bscan_2D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should see a combined output file ``cylinder_Bscan_2D_merged.out``. The tool will ask you if you want to delete the original single A-scan output files or keep them.\n",
    "\n",
    "### Plot the B-scan\n",
    "\n",
    "You can plot the B-scan using:\n",
    "\n",
    "    python -m tools.plot_Bscan user_models/cylinder_Bscan_2D_merged.out Ez"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import os\n",
    "from tools.plot_Bscan import get_output_data, mpl_plot\n",
    "\n",
    "filename = os.path.join(os.pardir, os.pardir, 'user_models', 'cylinder_Bscan_2D_merged.out')\n",
    "rxnumber = 1\n",
    "rxcomponent = 'Ez'\n",
    "outputdata, dt = get_output_data(filename, rxnumber, rxcomponent)\n",
    "plt = mpl_plot(filename, outputdata, dt, rxnumber, rxcomponent)\n",
    "\n",
    "# Change from the default 'seismic' colormap\n",
    "#plt.set_cmap('gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The B-scan (of the $E_z$ field component) shows the initial part of the signal (~0.5-1.5 ns) which represents the direct wave from transmitter to receiver. Then comes the refelected wave (~2-3 ns) from the metal cylinder which creates the hyperbolic shape."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
